package com.seekting.mynews.activity;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestManager;
import com.seekting.mynews.App;
import com.seekting.mynews.R;
import com.seekting.mynews.connect.tiangou.bean.response.TianGouGallery;
import com.seekting.mynews.connect.tiangou.bean.response.TianGouShowSrc;
import com.seekting.mynews.eventbus.TianGouShowEvent;
import com.seekting.mynews.manager.Action;
import com.seekting.mynews.manager.NewsManager;
import com.seekting.mynews.view.NewsViewPager;
import com.seekting.mynews.view.ScaleImageView;

import java.util.ArrayList;

import de.greenrobot.event.EventBus;

public class NewsDetailActivity extends Activity {

    public static final String EXTRA_INDEX = "index";
    public static final String EXTRA_LIST = "list";
    private ArrayList<TianGouGallery> list;
    private int fromIndex;
    private TianGouGallery tianGouGallery;
    private NewsManager mNewsManager;
    private ViewPager mViewPager;
    private PagerAdapter pagerAdapter;
    private ArrayList<TianGouShowSrc> srcs;
    private RequestManager mRequestManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mRequestManager = Glide.with(this);
        srcs = new ArrayList<TianGouShowSrc>();
        list = (ArrayList<TianGouGallery>) getIntent().getSerializableExtra(EXTRA_LIST);
        fromIndex = getIntent().getIntExtra(EXTRA_INDEX, 0);
        tianGouGallery = list.get(fromIndex);
        mNewsManager = App.getApp().getManager(NewsManager.class);
        EventBus.getDefault().register(this);
        Bundle bundle = new Bundle();
        bundle.putInt("id", tianGouGallery.getId());
        mNewsManager.request(Action.TianGouShow, bundle);
        mViewPager = new NewsViewPager(this);
        pagerAdapter = new PagerAdapter() {

            @Override
            public boolean isViewFromObject(View view, Object o) {
                return o == view;
            }

            @Override
            public int getCount() {
                return srcs.size();
            }

            @Override
            public void destroyItem(ViewGroup container, int position, Object object) {
                View v = (View) object;
                container.removeView(v);
            }

            @Override
            public Object instantiateItem(ViewGroup container, int position) {
                ScaleImageView scaleImageView = new ScaleImageView(NewsDetailActivity.this);
//                networkImageView.setDefaultImageResId(R.drawable.default_img);
                String url = getString(R.string.tngou_img_begin) + srcs.get(position).getSrc();
                mRequestManager.load(url).placeholder(R.drawable.default_img).into(scaleImageView);
                container.addView(scaleImageView, LinearLayout.LayoutParams.MATCH_PARENT,
                        LinearLayout.LayoutParams.MATCH_PARENT);
                return scaleImageView;

            }
        };
        mViewPager.setAdapter(pagerAdapter);
        setContentView(mViewPager);

    }

    public void onEventMainThread(TianGouShowEvent tianGouShowEvent) {

        srcs.addAll(tianGouShowEvent.getTianGouShow().getList());
        pagerAdapter.notifyDataSetChanged();

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
        mRequestManager.onDestroy();
    }
}
